package com.atguigu.axios;

import com.atguigu.pojo.User;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sun.org.apache.bcel.internal.generic.NEW;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @ClassName: Axios01Servlet
 * @Description:
 * @Author: wty
 * @Date: 2022/12/15
 */
@WebServlet("/axios02.do")
public class Axios02Servlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StringBuffer stringBuffer = new StringBuffer("");

        BufferedReader bufferedReader = request.getReader();
        String str = "";
        while ((str = bufferedReader.readLine()) != null) {
            stringBuffer.append(str);
        }
        str = stringBuffer.toString();
        System.out.println("str = " + str);

        // str = {"uname":"jack","pwd":"ok"}

        // 需要第三方jar包
        // 方式1
        Gson gson = new Gson();

        // 方式2:可以格式化
        //Gson gson1 = new GsonBuilder().create();

        User user = gson.fromJson(str, User.class);
        System.out.println(user);


    }
}
